package com.qc.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.core.annotation.Order;

import com.qc.entity.OrderEntity;
import com.qc.entity.UserEntity;
import com.qc.serverlistener.CacheUtil;

public class SMSClient {
	
	/** 发送短信 **/
	public static boolean send(String mob, String msg) {
		String str = "";
        try {
        	// 创建HttpClient实例     
            HttpClient httpclient =new DefaultHttpClient();
           
             //构造一个post对象
             HttpPost httpPost = new HttpPost("http://h.1069106.com:1210/Services/MsgSend.asmx/SendMsg");
             //添加所需要的post内容
             List<NameValuePair> nvps = new ArrayList<NameValuePair>();
             nvps.add(new BasicNameValuePair("userCode", "QCWLCF"));
             nvps.add(new BasicNameValuePair("userPass", "QCWLcf0830"));
             nvps.add(new BasicNameValuePair("DesNo", mob));
             nvps.add(new BasicNameValuePair("Msg", msg));
             nvps.add(new BasicNameValuePair("Channel", "0"));
              

             httpPost.setEntity( new UrlEncodedFormEntity(nvps, "UTF-8") );
             HttpResponse response = httpclient.execute(httpPost);
             HttpEntity entity = response.getEntity();
             if (entity != null) {    
            	 InputStream instreams = entity.getContent();    
            	 str = convertStreamToString(instreams);  
                 System.out.println(str);  
             }  
            
//        	HttpRequestSender httpRequest = new HttpRequestSender();
//        	//发送 POST 请求
//        	 String sr=WeixinUtil.httpRequest(
//        			 "http://h.1069106.com:1210/Services/MsgSend.asmx/SendMsg", 
//        			 "POST",
//             		 "userCode=XXXXX&userPass=XXXXX&DesNo="+mob+"&Msg="+msg+"&Channel=1");
       
            Document doc = null;
            doc = DocumentHelper.parseText(str); // 将字符串转为XML

            if (doc == null ) return false;
            Element rootElt = doc.getRootElement(); // 获取根节点
            if (rootElt == null ) return false;
            //System.out.println("根节点：" + rootElt.getName()); // 拿到根节点的名称
            System.out.println("根节点的值：" + rootElt.getText()); // 拿到根节点的名称
            if (rootElt.getText() == null || "".equals(rootElt.getText())) return false;
            if (Long.parseLong(rootElt.getText()) > 0 ) {
            	return true;
            } else {
            	return false;
            }
           
        } catch (DocumentException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
	}
	
	public static String convertStreamToString(InputStream is) {      
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));      
        StringBuilder sb = new StringBuilder();      
       
        String line = null;      
        try {      
            while ((line = reader.readLine()) != null) {  
                sb.append(line + "\n");      
            }      
        } catch (IOException e) {      
            e.printStackTrace();      
        } finally {      
            try {      
                is.close();      
            } catch (IOException e) {      
               e.printStackTrace();      
            }      
        }      
        return sb.toString();      
    }  

	
	/**
	 * BOSS下单并支付成功后
	 */
	public static void bossSendOrderAndDonePay(OrderEntity orderEntity) {
		
		UserEntity secretaryEntity = CacheUtil.getUserInfo(orderEntity.getSecretaryUserId());
		UserEntity bossEntity = CacheUtil.getUserInfo(orderEntity.getBossUserId());
		
		//BOSS支付成功后提醒秘书
		String codeStr = secretaryEntity.getNickname()+"很荣幸通知您："+bossEntity.getNickname()+"已对您的服务下单付款，请打开圈城app查看详情，并尽快确认是否接受该订单！如有疑问，请联系客服："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(secretaryEntity.getMobile(), codeStr);
		
		//BOSS支付成功后提醒BOSS
		String complateAddress = orderEntity.getShopProvince()+orderEntity.getShopCity()+orderEntity.getShopAddress();
		codeStr = "您的订单已生成，订单内容：聘用"+secretaryEntity.getNickname()+""+orderEntity.getTimeDay()+"，开始时间："+orderEntity.getStartDate()+"，地点："+complateAddress+"。目前订单处于等待确认状态，如有疑问，请联系客服："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(bossEntity.getMobile(), codeStr);
	}
	
	/**
	 * 秘书接受订单后
	 */
	public static void secReceiveOrder(OrderEntity orderEntity) {
		UserEntity secretaryEntity = CacheUtil.getUserInfo(orderEntity.getSecretaryUserId());
		UserEntity bossEntity = CacheUtil.getUserInfo(orderEntity.getBossUserId());
		
		//秘书
		String complateAddress = orderEntity.getShopProvince()+orderEntity.getShopCity()+orderEntity.getShopAddress();
		String codeStr = "您已接受"+bossEntity.getNickname()+"的聘用请求，请您再次核对订单内容：订单编号"+orderEntity.getOrderNumber()+"，聘用时长"+orderEntity.getTimeDay()+"，开始时间："+orderEntity.getStartDate()+"，地点："+complateAddress+"。如有疑问，请联系客服："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(secretaryEntity.getMobile(), codeStr);
		
		//boss
		codeStr = "您已成功聘用"+secretaryEntity.getNickname()+"。请您再次核对订单内容：订单编号"+orderEntity.getOrderNumber()+"，聘用时长"+orderEntity.getTimeDay()+"，开始时间："+orderEntity.getStartDate()+"，地点："+complateAddress+"。建议您提前预定场地。如有疑问，请联系客服："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(bossEntity.getMobile(), codeStr);
		
	}
	
	/**
	 * 秘书拒绝接单后
	 */
	public static void secCancelOrder(OrderEntity orderEntity) {
		UserEntity secretaryEntity = CacheUtil.getUserInfo(orderEntity.getSecretaryUserId());
		UserEntity bossEntity = CacheUtil.getUserInfo(orderEntity.getBossUserId());
		
		//秘书
		String codeStr = "您已拒绝"+bossEntity.getNickname()+"的下单聘请。如有疑问，请联系客服："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(secretaryEntity.getMobile(), codeStr);
		
		//boss
		codeStr = "很遗憾通知您，"+secretaryEntity.getNickname()+"拒绝了您的聘用请求，费用已原路退回至您支付的账户。如有疑问，请联系客服："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(bossEntity.getMobile(), codeStr);
	}
	
	/**
	 * 订单生成后BOSS取消订单时对BOSS的提醒
	 */
	public static void bossCancelOrder(OrderEntity orderEntity) {
		UserEntity secretaryEntity = CacheUtil.getUserInfo(orderEntity.getSecretaryUserId());
		UserEntity bossEntity = CacheUtil.getUserInfo(orderEntity.getBossUserId());
		
		//秘书
		String codeStr = "很遗憾通知您，"+bossEntity.getNickname()+"取消了编号为"+orderEntity.getOrderNumber()+"的订单。如有疑问，请联系客服："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(secretaryEntity.getMobile(), codeStr);
		
		//boss
		codeStr = "您已取消编号为"+orderEntity.getOrderNumber()+"的订单，费用已原路退回至您支付的账户。如有疑问，请联系客服："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(bossEntity.getMobile(), codeStr);
	}
	
	/**
	 * 订单开始前一天提醒
	 */
	public static void beginOrder(OrderEntity orderEntity) {
		UserEntity secretaryEntity = CacheUtil.getUserInfo(orderEntity.getSecretaryUserId());
		UserEntity bossEntity = CacheUtil.getUserInfo(orderEntity.getBossUserId());
		
		//秘书
		String codeStr = "您编号为"+orderEntity.getOrderNumber()+"的订单将于明天生效计时，请您安排好出发时间，在订单完成过程中请注意人身安全并保管好个人财物。您有权拒绝客户提出的超出订单以外的工作内容。如有需要，请及时与我们联系："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(secretaryEntity.getMobile(), codeStr);
		
		//boss
		codeStr = "您编号为"+orderEntity.getOrderNumber()+"的订单将于明天生效计时，请您安排好出发时间，并提前预定场地。如有疑问，请联系客服："+Constants.SERVER_PHONE+"【圈城】";
		SMSClient.send(bossEntity.getMobile(), codeStr);
	}
}
